/**
 * Copyright (C), 2001-2022, www.bosssof.com.cn
 * FileName: ThreadArrayListDemo
 * Author: Administrator
 * Date: 2022-10-24 15:07:28
 * Description:
 * <p>该文件对应java基础作业 4)	ThreadArrayListDemo.java
 * History:
 * <author> Administrator
 * <time> 2022-10-24 15:07:28
 * <version> 1.0.0
 * <desc> 作业初始化的版本未来将会更迭
 */
package com.bosssoft.hr.train.j2se.example.collection;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @ClassName: ThreadArrayListDemo
 * @Description:  完成集合框架作业多线程操作
 * @Author: Administrator
 * @Date: 2022-10-24 15:07
 **/
@Slf4j
public class ThreadArrayListDemo {
    /**
     * 测试数据 公共数据模拟并发访问
     */
    private List<String> productList=new ArrayList<>();
    /**
     * 测试生产者和消费者进行数据操作
     */
    public void startTesting() throws InterruptedException {

        /**
         * 用于控制多线程的执行完毕统计
         */
        CountDownLatch countDownLatch=new CountDownLatch(2);
        // 生成了同步的列表，你可以选择  synchronized 或者 ReentrantReadWriteLock 读写方式
        // 本范例采用了列表转化 建议同学可以阅读集合框架章节去了解并发控制
        List<String> syncProductList= Collections.synchronizedList(productList);
        // 启动生产者
        new ProducerThread(syncProductList,countDownLatch).start();
        //启动消费者
        new Thread(new ConsumerRunnable(syncProductList,countDownLatch)).start();

        // 这里组织该方法退出指导所有的countDownLatch->countDown 使引用计数为0
        countDownLatch.await();
    }
    /**
     * 生产者
     */
    public class ProducerThread extends Thread{
        /**
         *  计数
         */
        private CountDownLatch countDownLatch;
        /**
         *  产品列别
         */
        private List<String> productList;

        /**
         *
         * @param productList
         * @param countDownLatch
         */
        public  ProducerThread(List<String> productList, CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.productList=productList;
        }
        @Override
        public void run() {
            super.run();
            int i=1;
            try {
                while (true){

                    productList.add("product"+i);
                    log.info("加入","product"+i);
                    Thread.sleep(100);
                    i++;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        }
    }

    /**
     * 消费者线程
     */
    public class ConsumerRunnable implements Runnable{
        /**
         * 计数
         */
        private CountDownLatch countDownLatch;
        /**
         * 产品列表
         */
        private List<String> productList=new ArrayList<>();
        /**
         *
         * @param countDownLatch
         */
        public ConsumerRunnable(List<String>productList, CountDownLatch countDownLatch){
            this.countDownLatch=countDownLatch;
            this.productList=productList;
        }
        @Override
        public void run() {
            try {
                while (true){
                    if(!productList.isEmpty()){
                        log.info("移除",productList.get(0));
                        productList.remove(0);
                    }else{
                        log.info("没有数据可以删除");
                    }
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                this.countDownLatch.countDown();
            }

        }
    }
}
